﻿using EntityModels.Entities;
using Microsoft.EntityFrameworkCore;

namespace EntityModels.DbContexts
{
    /// <summary>
    /// 备用键
    /// </summary>
    public class AlternateDbContext : DbContext
    {
        public DbSet<AlternateKeyEntity> AlternateKeyEntities => Set<AlternateKeyEntity>();

        public DbSet<PrincipalKeyEntity> PrincipalKeyEntities => Set<PrincipalKeyEntity>();

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AlternateKeyEntity>()
                .HasAlternateKey(p => p.AlternateKey)
                .HasName("AlternateKey");

            modelBuilder.Entity<AlternateKeyEntity>()
                .HasOne(a => a.PrincipalKeyEntity)
                .WithMany()
                .HasPrincipalKey(a => a.PrincipalKey);
        }

    }
}
